package com.wenchao.quickstart.utils;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangwenchao
 */
public class AppUtil {

    /**
     * 获取APP版本名称
     *
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageManager manager = context.getPackageManager();
        String versionName = null;
        try {
            PackageInfo packageInfo = manager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 重启APP
     */
    public static void restartApp(Context context) {
        LogUtil.e("执行重启");
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 3000, restartIntent);
        new Thread(() -> {
            try {
                Thread.sleep(100);
                System.exit(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    /**
     * 根据包名跳转到某App
     *
     * @param activity    Activity
     * @param packageName 包名
     */
    public static void goApp(Activity activity, String packageName) {
        PackageManager packageManager = activity.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(packageName);
        if (intent == null) {
            Toast.makeText(activity, "未安装该应用", Toast.LENGTH_LONG).show();
        } else {
            activity.startActivity(intent);
        }
    }

    /**
     * 获取所有已安装的应用列表
     *
     * @param activity Activity
     * @return List<PackageInfo>
     */
    public static List<PackageInfo> getAppList(Activity activity) {
        PackageManager pm = activity.getPackageManager();
        return pm.getInstalledPackages(0);
    }

    /**
     * 通过包名获取PackageInfo
     *
     * @param activity    activity
     * @param packageName packageName
     * @return PackageInfo
     */
    public static PackageInfo getAppByPackageName(Activity activity, String packageName) {
        PackageManager pm = activity.getPackageManager();
        try {
            return pm.getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取所有已安装的应用列表
     *
     * @param activity Activity
     * @return List<PackageInfo>
     */
    public static List<PackageInfo> getNotSystemAppList(Activity activity) {
        PackageManager pm = activity.getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        List<PackageInfo> list = new ArrayList<>();
        for (PackageInfo packageInfo : packages) {
            // 判断系统/非系统应用
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                list.add(packageInfo);
            }
        }
        return list;
    }

    /**
     * 卸载应用
     */
    public static void uninstallApp(Context context, String pkg) {
        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:" + pkg));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 批量卸载
     *
     * @param packageNames
     */
    public static void batchUninstallApp(Context context, List<String> packageNames, UninstallResultCallback callback) {
        for (String packageName : packageNames) {
            int result = PackageUtils.uninstallSilent(context, packageName);
            if (result == 1) {
                callback.onSuccess(packageName);
            } else {
                callback.onFailed(packageName);
            }
        }
    }

    public interface UninstallResultCallback {

        void onSuccess(String packageName);

        void onFailed(String packageName);
    }

    private static void uninstall(Context context, String packageName) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent sender = PendingIntent.getActivity(context, 0, intent, 0);
        PackageInstaller mPackageInstaller = context.getPackageManager().getPackageInstaller();
        mPackageInstaller.uninstall(packageName, sender.getIntentSender());
    }

}
